﻿using BaseAuthRole.Domain.Input;
using BaseAuthRole.Domain.OutPut;
using BaseAuthRole.DTO.Role;
using BaseAuthRole.IService;
using BaseAuthRole.Web.Filters;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RS.BaseModel.Common;

namespace BaseAuthRole.Web.Controllers
{
    [Route("[controller]/[action]")]
    [PageAuthCheckFilter]
    [RoleActionCheckFilter]
    public class RoleManagerController : Controller
    {
        private IUserService _userService;
        public RoleManagerController(IUserService userService)
        {
            this._userService = userService;
        }
        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 角色详情
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleDetail()
        {
            return View();
        }

        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <returns></returns>
        public ActionResult EditRole(string id)
        {



            if (!string.IsNullOrEmpty(id))
            {
                var role = _userService.GetRole(id);

                ViewData["role"] = role;

            }

            return View();
        }

        #region 请求部分

        public string GetRole(int pagenum,int pagesize,string para)
        {

            var result = new CommonResult();

            if (pagesize < 0)
            {

                pagesize = 1;
            }
            if (pagesize > 200)
            {
                pagesize = 200;

            }
            if (pagenum <= 0)
            {

                pagenum = 1;
            }

            long count = 0;

            var condition = "";

            Dictionary<string,object> obj = new Dictionary<string, object>();


            if (!string.IsNullOrEmpty(para))
            {
                obj.Add("para", $"%{para}%");


                condition = " \"Name\" like @para";
            }
            var info = this._userService.GetRoles(pagenum, pagesize, condition, " \"CreateTime\" desc", out count, obj);

            var total_num = Math.Ceiling(count / (decimal)pagesize);


            result.IsError = false;
            result.Result = new
            {

                content = info,
                total_num = total_num

            };
            return JsonConvert.SerializeObject(result);
        }

        [HttpPost]
        public string EditRoleDo([FromForm]EditRoleDoInput editRoleDoInput)
        {
            var result = new CommonResult();

            var id = editRoleDoInput.Id;

            var name = editRoleDoInput.Name;

            var des = editRoleDoInput.Des;

            var data = editRoleDoInput.Data;

            var actions =string.IsNullOrEmpty(data)?new List<string>():data.Split(',').ToList();

            if (!string.IsNullOrEmpty(id))
            {

                var role = this._userService.GetRole(id);

                if (role == null)
                {
                    result.Message = "角色不存在";
                    return JsonConvert.SerializeObject(result);
                }

                role.Name = name;
                role.Description = des;

                var r = this._userService.UpdateRoleAction(role, actions);

                if (r > 0)
                {

                    result.IsError = false;
                }

            }
            else
            {

                var role = new Role
                {
                    IsSystem = 0,
                    Name = name,
                    Description = des


                };
                var r = this._userService.CreateRoleAction(role, actions);

                if (r > 0)
                {

                    result.IsError = false;
                }
            }
            return JsonConvert.SerializeObject(result);
        }

        public string GetRoleInfo(string id)
        {
            var result = new CommonResult();

            var actions = new List<string>();

            if (!string.IsNullOrEmpty(id))
            {
                actions = this._userService.GetRoleAction(id);
            }
            
            var ActionGroup = this._userService.GetActionGroup();

            var data = new List<ActionTree>();

            foreach (var o in ActionGroup)
            {
                data.Add(new ActionTree
                {
                    id = o.Id,
                    parent = "#",
                    text = o.Name

                });

                foreach (var p in o.Actions)
                {
                    data.Add(new ActionTree
                    {
                        id= p.Id,
                        parent = o.Id,
                        text = p.ActionName,
                        state = new ActionTreeState
                        {
                            selected = actions.Any(m => m == p.Id)
                        }
                    });
                }
            }
         
            result.IsError = false;

            result.Result = new { data = data };

            return JsonConvert.SerializeObject(result);

        }


        #endregion

    }
}
